AddCSLuaFile("shared.lua")
AddCSLuaFile("Config/sh_config.lua")
AddCSLuaFile("Config/sh_lang.lua")
AddCSLuaFile("cl_init.lua")
AddCSLuaFile('gui/cl_hud.lua')
AddCSLuaFile('gui/cl_menu.lua')
AddCSLuaFile('gui/cl_skin.lua')
include("shared.lua")
include("Entity/Player.lua")

function GM:PlayerSelectSpawn( Ply )
	local Spawns = ents.FindByClass( "info_player_start" )
	local RandomEntry = math.random(#ents.FindByClass( "info_player_start" ))
	if Ply:Team() == TEAM_WARRIORS then
		Spawns = ents.FindByClass( "info_player_terrorist" )
		if Ply.VarSpawn then
			RandomEntry = Spawns[VAR.Spawn[Ply:UniqueID()]]
		else
			RandomEntry = math.random(#Spawns)
		end
	elseif Ply:Team() != TEAM_SPECTATORS then

		Ply:SetTeam(TEAM_SPECTATORS)
	end
    return Spawns[RandomEntry]
end
function GM:PlayerSetModel( ply )
 util.PrecacheModel( "models/player/Group03/male_07.mdl" )
        ply:SetModel( "models/player/Group03/male_07.mdl"  )
end
function GM:PlayerSpawn(Ply)
	 util.PrecacheModel( "models/player/Group03/male_07.mdl" )
        Ply:SetModel( "models/player/Group03/male_07.mdl"  )
	self.BaseClass:PlayerSpawn( Ply ) 
    Ply:SetGravity( 0.78 )  --0.75
	Ply:SetRunSpeed(Config.DefaultRunSpeed)
	Ply:SetCanZoom( false )
	if	(Ply.scale) then
		Ply.scale = 1
	end
	hook.Call("PlayerLoadout", GAMEMODE, Ply)
end
function GM:KeyPress(Ply, Key)
	if Key == IN_ZOOM then
		//local tr =  Ply:GetEyeTrace()
		local spell = ents.Create("spell_fireball")
		spell:SetPos(Ply:EyePos() + Ply:EyeAngles():Forward() * 64 + Vector( 0, 0, 0 ))
		spell:SetAngles(Angle(0, 0, 0))
		spell:Spawn()
		spell:Call(Ply)
	end
	
end
function GM:PlayerLoadout(Ply)
	if Ply:Team() == TEAM_WARRIORS then
		Ply:Give("gma_knife")
		Ply:Give("gmod_tool")
		Ply:Give("gmod_camera")
	else
		Ply:StripWeapons()
	end
	return true
end
concommand.Add("statuss", function(ply) 
	if team.NumPlayers(TEAM_WARRIORS) < VAR.MinWarriors then
		ply:ChatPrint("Le match est en attente de joueurs")
	end 
	if VAR.MatchStarted then
		ply:ChatPrint("Le match est en cours")
	end
	if VAR.AccountingStarted then
		ply:ChatPrint("Le match va commené")
	end
end)
function GM:StartMatch()
	if team.NumPlayers(TEAM_WARRIORS) < VAR.MinWarriors then
		VAR.AccountingStarted = false
		VAR.MatchStarted = false
		Callhandler("end")
		for k, v in pairs(player.GetAll()) do
			v:ChatPrint(ChosenLang.NotEnoughtWarriors)
			v:Spawn()
		end
		self:SendInfoForHud()
		return
	end
	VAR.MatchStarted = true

	Callhandler("start")

	VAR.AccountingStarted = false
	for k, v in pairs(player.GetAll()) do
		v:ChatPrint(ChosenLang.MatchBegin)
	end
	self:SendInfoForHud()
end
function GM:PlayerDeath(Ply)
	Ply:SetTeam(TEAM_SPECTATOR)
	Ply:Spawn()
end
function GM:Think()
	if VAR.MatchStarted then
		if team.NumPlayers(TEAM_WARRIORS) < 2 then
			winner = team.GetPlayers(TEAM_WARRIORS)[1]
			if winner:IsPlayer() then
				winner:ChatPrint("Vous avez gagné")
				winner:Spawn()
			end
			Callhandler("end")
			VAR.MatchStarted = false
		end
	elseif !VAR.AccountingStarted and team.NumPlayers(TEAM_WARRIORS) >= VAR.MinWarriors then
		timer.Simple(Config.AccouningLenght, self.StartMatch, self)
		VAR.AccountingStarted = true
		Callhandler("accounting")
		self:SendInfoForHud()
	end
end
function GM:InitPostEntity()
	timer.Simple(10, self.manageSpawners, self)
end
function GM:manageSpawners()
	local frequence = math.Clamp(20/(team.NumPlayers(TEAM_WARRIORS)), 1, 10)
	local Ents = ents.FindByClass( "gma_spawner" )
	local ChosenEnt = Ents[math.random(#Ents)]
	ChosenEnt:spawnRandomShit()
	timer.Simple(frequence, self.manageSpawners, self)
end

function Callhandler(Type)
	for k, v in pairs(ents.FindByClass("gma_eventshandler")) do
		if Type == "start" then
			v:TriggerMatchStarted()
		elseif Type == "end" then
			v:TriggerMatchEnded()
		elseif Type == "accounting" then
			v:TriggerAccountingStarted()
		end
	end
end
function GM:PlayerShouldTakeDamage( victim, Ply )
	if Ply:IsPlayer() then
		if !VAR.MatchStarted then return false end
	end
	return true 
end
function GM:SendInfoForHud()
	umsg.Start( "HUDreceiver" )
		umsg.Bool(VAR.MatchStarted)
		umsg.Bool(VAR.AccountingStarted)
		umsg.Float(CurTime())
	umsg.End()
end
function GM:ShowSpare2()
	umsg.Start( "F4Menu" )
	umsg.End()
end
function GM:EntityTakeDamage( ent, inflictor, attacker, amount, dmginfo )
   if ent:IsPlayer() then
     local scale = ent.scale or 1
	ent:ChatPrint('damage : ' .. dmginfo:GetDamage())
	  dmginfo:ScaleDamage( 1 * scale )
	  ent:ChatPrint('scaled to : ' .. dmginfo:GetDamage())
   end
end